package com.hm.system.service.mongo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.hm.common.model.PageInfo;
import com.hm.system.mongo.domain.def.SysDictDef;
import com.hm.system.mongo.domain.system.QSysDictInfo;
import com.hm.system.mongo.domain.system.SysDictInfo;
import com.hm.system.mongo.vo.system.SysDictInfoVo;
import com.hm.system.share.repository.mongo.system.SysDictInfoRepository;
import com.hm.system.share.service.mongo.system.SysDictInfoService;
import com.mysema.query.types.expr.BooleanExpression;

/**
 * @author shishun.wang
 * @date 2016年6月2日 下午11:01:40
 * @version 1.0
 * @describe 
 */
@Service
@SuppressWarnings({ "rawtypes", "unchecked" })
public class SysDictInfoServiceImpl implements SysDictInfoService {
	
	@Autowired
	private SysDictInfoRepository sysDictInfoRepository;

	@Override
	public String addSysDictInfo(SysDictInfo dictInfo, String operationUser) {
		dictInfo.setCreateUser(operationUser);
		return sysDictInfoRepository.save(dictInfo).getId();
	}

	@Override
	public String updateSysDictInfo(SysDictInfo dictInfo, String operationUser) {
		return sysDictInfoRepository.save(dictInfo).getId();
	}

	@Override
	public String deleteSysDictInfo(SysDictInfo dictInfo, String operationUser) {
		sysDictInfoRepository.delete(dictInfo.getId());
		return dictInfo.getId();
	}

	@Override
	public SysDictInfo getSysDictInfo(String id) {
		return sysDictInfoRepository.findOne(id);
	}

	@Override
	public List<SysDictInfo> getDictsByDictDef(SysDictDef dictDef) {
		return (List<SysDictInfo>) sysDictInfoRepository.findAll(QSysDictInfo.sysDictInfo.dictDef.eq(dictDef));
	}

	@Override
	public PageInfo<SysDictInfoVo> querySysDictInfo(String name, String code, SysDictDef dictDef, Long startTime,
			Long endTime, Integer pageNumber, Integer pageSize) {
		PageInfo<SysDictInfoVo> pageInfo = new PageInfo<SysDictInfoVo>(pageNumber, pageSize);
		QSysDictInfo qSysDictInfo = QSysDictInfo.sysDictInfo;
		Pageable pageable = new QPageRequest(pageInfo.getPageNumber(), pageInfo.getPageSize(),
				qSysDictInfo.createTime.desc());
		
		BooleanExpression predicate = null;
		
		List<SysDictInfoVo> sysDictInfoVos = new ArrayList<SysDictInfoVo>();
		Page page = null;
		if(!StringUtils.isEmpty(name)){
			predicate = qSysDictInfo.name.like(name);
			
			if(!StringUtils.isEmpty(code)){
				predicate.and(qSysDictInfo.code.like(code));
			}
			
			if(null!=(dictDef)){
				predicate.and(qSysDictInfo.dictDef.eq(dictDef));
			}
			
			if(null!=startTime&&null!=endTime){
				predicate.and(qSysDictInfo.createTime.between(startTime, endTime));
			}else if(null!=startTime&&null==endTime){
				predicate.and(qSysDictInfo.createTime.goe(startTime));
			}else if(null==startTime&&null!=endTime){
				predicate.and(qSysDictInfo.createTime.loe(startTime));
			}
			
			page = sysDictInfoRepository.findAll(predicate, pageable);
		}else if(!StringUtils.isEmpty(code)){
			predicate = qSysDictInfo.code.like(code);
			if(null!=(dictDef)){
				predicate.and(qSysDictInfo.dictDef.eq(dictDef));
			}
			
			if(null!=startTime&&null!=endTime){
				predicate.and(qSysDictInfo.createTime.between(startTime, endTime));
			}else if(null!=startTime&&null==endTime){
				predicate.and(qSysDictInfo.createTime.goe(startTime));
			}else if(null==startTime&&null!=endTime){
				predicate.and(qSysDictInfo.createTime.loe(startTime));
			}
			
			page = sysDictInfoRepository.findAll(predicate, pageable);
		}else if(null!=dictDef){
			predicate = qSysDictInfo.dictDef.eq(dictDef);
			
			if(null!=startTime&&null!=endTime){
				predicate.and(qSysDictInfo.createTime.between(startTime, endTime));
			}else if(null!=startTime&&null==endTime){
				predicate.and(qSysDictInfo.createTime.goe(startTime));
			}else if(null==startTime&&null!=endTime){
				predicate.and(qSysDictInfo.createTime.loe(startTime));
			}
			
			page = sysDictInfoRepository.findAll(predicate, pageable);
		}else if(null!=startTime&&null!=endTime){
			predicate = qSysDictInfo.createTime.between(startTime, endTime);
			
			page = sysDictInfoRepository.findAll(predicate, pageable);
		}else if(null!=startTime&&null==endTime){
			predicate = qSysDictInfo.createTime.goe(startTime);
			
			page = sysDictInfoRepository.findAll(predicate, pageable);
		}else if(null==startTime&&null!=endTime){
			predicate = qSysDictInfo.createTime.loe(startTime);
			
			page = sysDictInfoRepository.findAll(predicate, pageable);
		}else{
			page = sysDictInfoRepository.findAll(pageable);
		}

		
		List<SysDictInfo> content = page.getContent();
		for (SysDictInfo sysDictInfo : content) {
			SysDictInfoVo sysDictInfoVo = new SysDictInfoVo();
			{
				sysDictInfoVo.setId(sysDictInfo.getId());
				sysDictInfoVo.setName(sysDictInfo.getName());
				sysDictInfoVo.setCode(sysDictInfo.getCode());
				sysDictInfoVo.setCreateTime(sysDictInfo.getCreateTime());
				sysDictInfoVo.setCreateUser(sysDictInfo.getCreateUser());
				
				sysDictInfoVo.setDictDef(sysDictInfo.getDictDef());
			}

			sysDictInfoVos.add(sysDictInfoVo);
		}

		return pageInfo.execute(page, sysDictInfoVos);
	}

}
